package com.hnucm.cooklife.presenter.friend;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.User;
import com.hnucm.cooklife.contract.friends.Contract;
import com.hnucm.cooklife.model.FriendModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class FriendPresenter extends BasePresenter<Contract.IFriendView> implements Contract.IFriendPresenter {
    Contract.IFriendModel mIFriendModel;

    public FriendPresenter() {
        mIFriendModel = new FriendModel();
    }

    @Override
    public void loadFriend(int userId) {
        mIFriendModel.loadFriend(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull User user) {
                        if(isViewAttached()){
                            getView().onLoadFriend(user);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage()+"friend");
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshFriend(int userId) {

    }
}
